<?php
class Model_Privilege extends Zend_Db_Table_Abstract
{
    protected $_name = 'privileges';
    protected $_primary = 'id';
    protected  $_order = 'id';

    public function getViewByArrRoleID($arrRole){
		$query = 'Select views FROM privileges where views != "" and roleId IN ('. implode(',', $arrRole).')';
        $query = $this->getDefaultAdapter()->query( $query );
		return $query->fetchAll(PDO::FETCH_OBJ);
    }
    public function deleteDataByProjectID($projectId){
        $this->delete('projectId =  ' . (int)($projectId));
    }
    public function addMultiData($dataPrivileges){
        $sql = 'INSERT INTO privileges (roleId, projectId, resourceId, views) VALUES ' . implode(',', $dataPrivileges);
        
        $privileges = $this->getAdapter()->prepare($sql);
		$privileges->execute();
    }
    public function getViewByRoleId($roleId)
    {
		$query = $this->select()
						->from(array('s' => "privileges" ),array('views'))
                		->where("roleId = ?", $roleId);
                        
        return $this->fetchAll($query);
    }
    //chua su dung

    /*******INSERT*******/
    public function insertItem($info)
    {
        $this->insert($info);
        return $this->_identity;
    }

    public function insertItems($arrayItems)
    {
        $db = Zend_Db_Table::getDefaultAdapter();
        $items = '';
        foreach ($arrayItems as $r) {
            $items .= "({$r['roleId']},{$r['projectId']},{$r['resourceId']},{$db->quote($r['views'])}),";
        }

        $stmt = 'INSERT INTO privileges (roleId,projectId,resourceId,views) VALUES ' . substr($items, 0,-1);
        $db->query($stmt);
    }

    /*******UPDATE*******/
    public function updateItem($id,$info)
    {
        $this->update( $info, "id=" . (int) $id );
    }

    /*******DELETE*******/
    public function deleteItem($id)
    {
        $this->delete( "id=" . (int) $id );
    }

    /*******GET ITEMS*******/
    public function getItems($option = array())
    {
        $count = isset($option['count'])? $option['count'] : 0;
        $offset = isset($option['offset'])? $option['offset'] : 0;
        $order = isset($option['order'])? $option['order'] : $this->_order;
        $sort = isset($option['sort'])? $option['sort'] : 'asc';
        $query = $this->select()->from(array('s' => "privileges" ),
                        array(
                            'id',
                            'roleId',
                            'projectId',
                            'resourceId'
                        ))
                ->order("{$order} {$sort}")
                ->limit($count,$offset);
        $results = $this->fetchAll($query);
        if ( !$results )
        {
            throw new Exception( 'Could not query : {}');
        }
        return $results;
    }

    /*******GET ITEM*******/
    public function getItem($id)
    {
        $query = $this->select()->from(array('s' => "privileges" ),
                        array(
                            'id',
                            'roleId',
                            'projectId',
                            'resourceId'
                        ))
                ->where("id = {$id}");
        $result = $this->fetchRow($query);
        if ( !$result )
        {
            throw new Exception( 'Could not query : {}');
        }
        return $result;
    }

    /*******COUNT ITEM*******/
    public function countItems()
    {
        $query = $this->select()
                ->from(privileges,'count(*) as total');
        $result = $this->fetchRow($query);
        return $result->total;
    }

    /**
     * @todo Get List Item Privileges By Role ID
     * @author NguyenNT
     * @param int roleId
     * @return list Privileges
     */
    public function getItemsByRoleId($roleId)
    {
        $stmt = $this->select()->from('privileges AS pr')
                                ->join('resources AS r', 'pr.resourceId = r.id',array('moduleName','controllerName','actionName'))
                                ->where('pr.roleId = ' . $roleId)
                                ->setIntegrityCheck( false );
        return $this->fetchAll($stmt);
    }

    public function getProjectsByRoleId($roleId)
    {
        $stmt = $this->select()->from('privileges AS pr',array())
                                ->join('project AS p', 'p.projectId = pr.projectId',array('projectId','projectName'))
                                ->where('pr.roleId = ' . $roleId)
                                ->group('p.projectId')
                                ->setIntegrityCheck( false );
        return $this->fetchAll($stmt);
    }

    public function getResourcesByRoleId($roleId)
    {
        $stmt = $this->select()->from('privileges AS pr',array('id AS privilegeId','resourceId','views'))
                                ->join('resources AS r', 'pr.resourceId = r.id',array('resourceName'))
                                ->where('pr.roleId = ' . $roleId)
                                ->setIntegrityCheck( false );
        return $this->fetchAll($stmt);
    }

}